package com.yeskery.nut.web.demo.event;

import com.yeskery.nut.annotation.bean.Component;
import com.yeskery.nut.annotation.event.TransactionalEventListener;
import com.yeskery.nut.transaction.TransactionPhase;
import com.yeskery.nut.web.demo.domain.Order;

import java.util.logging.Logger;

/**
 * 下单事务事件监听器
 * @author YESKERY
 * 2023/9/8
 */
@Component
public class TakeOrderTransactionalEventListener {

    /** 日志对象 */
    private static final Logger logger = Logger.getLogger(TakeOrderTransactionalEventListener.class.getName());

    @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT, value = TakeOrderTransactionalEvent.class)
    public void handleTakeOrderBeforeCommitEvent(TakeOrderTransactionalEvent event) {
        Order order = (Order) event.getSource();
        logger.info("订单id: " + order.getId() + "，事务提交前执行");
    }

    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT, value = TakeOrderTransactionalEvent.class)
    public void handleTakeOrderAfterCommitEvent(TakeOrderTransactionalEvent event) {
        Order order = (Order) event.getSource();
        logger.info("订单id: " + order.getId() + "，事务提交后执行");
    }

    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMPLETION, value = TakeOrderTransactionalEvent.class)
    public void handleTakeOrderAfterCompletionEvent(TakeOrderTransactionalEvent event) {
        Order order = (Order) event.getSource();
        logger.info("订单id: " + order.getId() + "，事务完成后执行");
    }

    @TransactionalEventListener(phase = TransactionPhase.AFTER_ROLLBACK, value = TakeOrderTransactionalEvent.class)
    public void handleTakeOrderAfterRollbackEvent(TakeOrderTransactionalEvent event) {
        Order order = (Order) event.getSource();
        logger.info("订单id: " + order.getId() + "，事务回滚后执行");
    }
}
